<template>
	<view>
		<!-- 顶部的搜索组件 -->
		<my-search @click="gotosearch"></my-search>
		<!-- 分类选择 -->
		<view class="cate-container">
			<!-- z左侧一级导航 -->
			<scroll-view class="cate-left" :style="{height:wh+'px'}" scroll-y>
				<view :class="['cate-left-item', active===i ? 'active' : '']" v-for="(item,i) in catelist " :key="i"
					@click="changeactive(i)">
					{{item.cat_name}}
				</view>
			</scroll-view>
			<!-- 右侧二级导航 -->
			<scroll-view class="cate-right" :style="{height:wh+'px'}" scroll-y :scroll-top="scrollTop">
				<view class="cate-right-item" v-for="(item,i) in catelist2" :key="i">
					<!-- title -->
					<view class="cate-right-item-title">
						/{{item.cat_name}}/
					</view>

					<view class="cate-right-item-content">
						<view class="cate-right-item-content-box" v-for="(item2,i2) in item.children" :key="i2"
							@click="gotoGoodsList(item2)">
							<img :src="item2.cat_icon" alt="" mode='widthFix'>
							<text>{{ item2.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	// 导入自己封装的 mixin 模块
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		// 将 badgeMix 混入到当前的页面中进行使用
		mixins: [badgeMix],
		data() {
			return {
				catelist: [],
				active: 0,
				// 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar 高度
				wh: 0,
				// 二级数据
				catelist2: [],
				scrollTop: 0,

			};
		},
		onLoad() {
			const sysInfo = uni.getSystemInfoSync()
			// 为wh赋值(windowHeight, 可使用窗口高度)
			this.wh = sysInfo.windowHeight
			this.getCatelist()
		},
		methods: {
			// 获取分类的数据
			async getCatelist() {
				const {
					data: res
				} = await uni.$http.get('/categories')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.catelist = res.message
				this.catelist2 = this.catelist[0].children
			},
			changeactive(i) {
				console.log(i)
				this.active = i
				this.catelist2 = this.catelist[i].children
				this.scrollTop = Math.random()
			},
			gotoGoodsList(cate) {
				uni.navigateTo({
					url: `/subpkg/goods_list/goods_list?cid=${cate.cat_id}`
				})
			},
			gotosearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		},
	}
</script>

<style lang="scss">
	.cate-container {
		display: flex;

		.cate-left {
			width: 240rpx;

			.cate-left-item {
				line-height: 120rpx;
				background-color: #f7f7f7;
				text-align: center;
				font-size: 24rpx;

				&.active {
					background-color: #ffffff;
					position: relative;

					&::before {
						content: ' ';
						display: block;
						width: 6rpx;
						height: 60rpx;
						background-color: #c00000;
						position: absolute;
						left: 0;
						top: 50%;
						transform: translateY(-50%);
					}
				}


			}
		}

		.cate-right {
			background-color: white;

			.cate-right-item {
				.cate-right-item-title {
					display: flex;
					justify-content: center;
					font-weight: bold;
					font-size: 24rpx;
					padding: 20rpx 0;
				}

				.cate-right-item-content {
					display: flex;
					flex-wrap: wrap;

					.cate-right-item-content-box {
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						width: 33.33%;
						padding: 20rpx;
						box-sizing: border-box;

						image {
							width: 100%;
						}

						text {
							font-size: 24rpx;
						}
					}
				}
			}
		}
	}
</style>
